pipeline {
    agent any
    environment {

        base_url='http://139.159.214.157'
        username='shuwenouwan'
        password='Shuwenouwan123'
        // 测试报告
        alluredir = '/reports/allure/allure-results'
        allure_reports = '/reports/allure/allure-reports'
        allure_reports_url = '139.159.214.157'
        allure_reports_url_port = '8083'

        //只测试标记为regression、function、compatibility的用例
        markers='function,regression,compatibility'

        // 项目地址
        project_url = 'https://gitee.com/shuwenouwan/himart-test.git'

        // Android/ios平台
        architecture = "['x86_64','arm64-v8a']"

        mobile_brand_android = "[['小米': ['xiaomi 6', 'xiaomi 10', 'xiaomi 13']],['华为': ['huawei P40', 'huawei Mate 40 Pro', 'huawei Nova 9']],['三星': ['samsung Galaxy S21', 'samsung Galaxy Note 20', 'samsung Galaxy Z Fold 3']],['oppo': ['oppo Find X3 Pro', 'oppo Reno6 Pro+', 'oppo A93']],['vivo': ['vivo X60 Pro', 'vivo Y76s', 'vivo iQOO Neo5']]]"
        platform_name = "['Android', 'ios']"
        app_url = "['himart-test/resources/app-file/himart.apk', 'himart-test/resources/app-file/himart.ipa']"
        device_name = "['device_functional_tests', 'device_integration_tests', 'device_compatibility_tests']"
        app_package_name = "['uni.UNI31459F4', 'uni.UNI31459F4']"
        app_activity_name = "['io.dcloud.PandoraEntry', 'io.dcloud.PandoraEntry']"
        automation_name = 'UiAutomator2'
        webdriver_url_appium = "${base_url}:8444/wd/hub"

        // web平台
        webdriver_url_selenium = "${base_url}:8444/wd/hub"
        browsers = 'chrome, edge, firefox'
        headless = true

        // 小程序
    }    
    stages {
        stage('构建测试环境|检查测试环境'){
            steps {
                script {

                    sh "rm -rf himart-test"
                    sh "git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git"
                    sh "echo '项目下载成功'"

                    def composeCommand = 'docker-compose -f himart-test/Docker/docker-compose.selenium_grid.yml'
                    // 检查 Selenium Grid Hub 容器是否已经启动
                    def containerStatus = sh(returnStdout: true, script: "${composeCommand} ps").trim()
                    // 获取所有容器的状态
                    def containers = containerStatus.split(/\n/)
                    // 检查所有容器是否都已经启动
                    def allContainersUp = true
                    for (def container in containers) {
                        if (!container.toLowerCase().contains('up')) {
                            sh "echo 'selenium容器未启动'"
                            allContainersUp = false
                            break
                        }
                    }
                    if (!allContainersUp) {
                        sh "${composeCommand} down"
                        sh "${composeCommand} up -d"
                        echo "selenium所有容器已重新启动."
                    } else {
                        echo "selenium所有容器已启动."
                    }
                }
            }
        }
        stage('并行测试') {
            parallel {
                stage('web平台:功能性测试|兼容性测试|回归测试') {
                    steps {
                        script {
                            //删除selenium-hub容器中的himart-test测试项目文件，并重新下载
                            sh "docker exec selenium-hub rm -rf himart-test"
                            sh "docker exec selenium-hub git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git"
                            // 【并行测试功能性测试|兼容性测试|回归测试】
                            def browsers = env.browsers.tokenize(',')
                            def markers = env.markers.tokenize(',')
                            for (marker in markers) { 
                                sh "当前测试：${marker}"
                                for (browser in browsers) { //【并行测试chrome、edge、Firefox】
                                    sh "当前测试：${browser}"
                                    sh "docker exec  selenium-hub pytest  -m ${marker} --alluredir=${alluredir} --allure_reports=${allure_reports}  --allure_reports_url=${allure_reports_url}  --allure_reports_url_port=${allure_reports_url_port}   --webdriver_url_selenium=${webdriver_url_selenium}   --browser=${browser}    /app/himart-test/tests/web_tests/tests/functional_tests/test_login.py" //jenins容器中调用selenium容器中pytest框架测试项目并生成报告
                                }
                            }
                        }
                    }
                }
                stage('mobile:功能性/兼容性/回归测试') {
                    steps {
                        echo "mobile."
                        // script {
                        //     //删除selenium-hub容器中的himart-test测试项目文件，并重新下载
                        //     sh "docker exec selenium-hub rm -rf himart-test"
                        //     sh "docker exec selenium-hub git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git"
                        //     for (marker in markers) { // 【功能性测试|兼容性测试|回归测试】
                        //         for (browser in browsers) { //【并行测试chrome、edge、Firefox】
                        //             sh "docker exec  selenium-hub pytest  -m ${marker} --alluredir=${alluredir} --allure_reports=${allure_reports}  --allure_reports_url=${allure_reports_url}  --allure_reports_url_port=${allure_reports_url_port}   --webdriver_url_selenium=${webdriver_url_selenium}   --browser=${browser}    /app/himart-test/tests/web_tests/tests/functional_tests/test_login.py"
                        //         }
                        //     }
                        // }


                        // script {
                        //     def composeCommand = 'docker-compose -f himart-test/Docker/docker-compose.appium_android.yml'
                        //     sh "${composeCommand} up -d"
                        //     //指定在docker-compose.selenium_grid.yml文件中命名为appium_android_pytest的容器中下载项目
                        //     sh "${composeCommand} exec appium_android_pytest git clone https://gitee.com/shuwenouwan/himart-test.git"  
                        //     //并行执行功能性、兼容性、回归测试 
                        //     for (marker in markers) { 
                        //         ////并行执行小米、华为、三星等手机品牌的功能性、兼容性、回归测试cc(真机测试情况下)
                        //         // for (items in mobile_brand_android) { 
                        //             ////"xiaomi 6", "xiaomi 10", "xiaomi 13"等小米手机型号
                        //             // for (item in items) { 
                        //                 sh """
                        //                     //指定在docker-compose.selenium_grid.yml文件中命名为appium_android_pytest的容器中执行测试代码并生成报告
                        //                     "${composeCommand} exec appium_android_pytest pytest -m ${marker}  --alluredir=${alluredir} --allure_reports=${allure_reports} --allure_reports_url=${allure_reports_url}:${allure_reports_url_port} \\
                        //                     --allure_reports_url_port=${allure_reports_url_port}
                        //                     --platform_name=${platform_name[0]} \\ 
                        //                     --automation_name=${automation_name} \\
                        //                     --app_url=${app_url[0]} \\
                        //                     --device_name=${device_name[0]} \\
                        //                     --app_package_name=${app_package_name[0]} \\
                        //                     --app_activity_name=${app_activity_name[0]} \\
                        //                     --webdriver_url_appium=${webdriver_url_appium} \\
                        //                     himart-test/tests/mobile_tests"   
                        //                 """
                        //             // }
                        //         // }
                        //     }
                        //     sh "${composeCommand} down"
                        // }
                    }
                }

                // stage('Api接口测试') {
                //     steps {
                //         error 'Api接口测试.'
                //         script {
                //             def composeCommand = 'docker-compose -f himart-test/Docker/docker-compose.newman.yml'
                //             sh "${composeCommand} up -d" 
                //             // 检查服务是否启动成功
                //             def serviceCheckCommand = "${composeCommand} ps"
                //             def serviceStatus = sh(script: serviceCheckCommand, returnStdout: true).trim()
                //             if (serviceStatus.contains('Up')) {
                //                 println '服务已启动成功.'
                //                 //指定在docker-compose.selenium_grid.yml文件中命名为newman的容器中下载项目
                //                 sh "${composeCommand} exec newman git clone https://gitee.com/shuwenouwan/himart-test.git"  
                //                 sh "${composeCommand} exec newman newman run /tmp/postman_collection.json --reporters cli,junit"
                //                 // 停止服务
                //                 sh "${composeCommand} down"
                //             } else {
                //                 error '服务启动失败.'
                //             }
                //         }
                //     }
                // }
                // stage('压力测试') {
                //     steps {
                //         error '压力测试.'
                //         script { 
                //             def composeCommand = 'docker-compose -f himart-test/Docker/docker-compose.jmeter.yml'
                //             sh "${composeCommand} up -d" 
                //             // 检查服务是否启动成功
                //             def serviceCheckCommand = "${composeCommand} ps"
                //             def serviceStatus = sh(script: serviceCheckCommand, returnStdout: true).trim()
                //             if (serviceStatus.contains('Up')) {
                //                 println '服务已启动成功.'
                //                 // 运行Newman测试
                //                 sh "${composeCommand} exec jmeter_container jmeter -n -t /jmx/test-plan.jmx -l /results/jmeter-results.csv --json /results/jmeter-results.json --allure /results/allure-jmeter"
                //                 // 停止服务
                //                 sh "${composeCommand} down"
                //             } else {
                //                 error '服务启动失败.'
                //             }
                //         }
                //     }
                // }
                // stage('安全性测试') {
                //     steps {
                //         echo "你好"
                //     }
                // }
            }
        }
    }
}